package _2022.hot100._49_字母异位词分组;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author： YHSimon
 * @date： 2022-04-13 17:11
 */
public class Solution {
    public List<List<String>> groupAnagrams(String[] strs){
        int len=strs.length;
        boolean[] use=new boolean[len];
        List<List<String>> ans=new ArrayList<>();
        for(int i=0;i<len;i++){
            List<String> tmp=new ArrayList<>();
            if(!use[i]){
                tmp.add(strs[i]);
                use[i]=true;
                for(int j=i+1;j<len;j++){
                    if(!use[j]){
                        if(judge(strs[i],strs[j])){
                            tmp.add(strs[j]);
                            use[j]=true;
                        }
                    }
                }
            }
            if(tmp.size()>0){
                ans.add(tmp);
            }

        }
        return ans;
    }


    public boolean judge(String a,String b){
        if(a.length()!=b.length()){
            return false;
        }
        char[] aChars = a.toCharArray();
        char[] bChars = b.toCharArray();
        Arrays.sort(aChars);
        Arrays.sort(bChars);
        for(int i=0;i<a.length();i++){
            if(aChars[i]!=bChars[i]){
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        Solution s = new Solution();
//        List<List<String>> lists = s.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
        List<List<String>> lists = s.groupAnagrams(new String[]{""});
        System.out.println(lists.toString());
    }
}
